<?php

namespace wxshop;

use think\facade\Db;
use think\facade\Request;

class ShopGoods extends Common
{
    public static $AccessToken = '';

    public function __construct($appid,$secret){
        if($appid && $secret){
            $this->access_token = $this->getAccessToken($appid, $secret);
        }else{
            return false;
        }
    }


    public function GoodsAdd($param)
    {
        $url = 'https://api.weixin.qq.com/channels/ec/product/add?access_token='.$this->access_token;
        $post['out_product_id'] = (string)$param['id'];
        $post['title'] = $param['name'];
        $request = Request::instance();
        $domain = $request->domain();
        $imgages = $param['images'];
        foreach ($param['images'] as $imgkey=>$img){
            $imgages[$imgkey] = $domain.$img;
        }

        $post['head_imgs'] = $imgages;
        $post['deliver_method'] = 0;
        $post['deliver_acct_type'] = [1,2,3,4];
        if(isset($param['detail_images'])){
            $detail_images = $param['detail_images'];
            foreach ($detail_images as $imgkey=>$img){
                $detail_images[$imgkey] = $domain.$img;
            }
            $post['desc_info']['imgs'] = $detail_images;
        }
        $post['cats'] = [];
        $cats_v2 = explode('-',$param['classify_id'][0]);
        foreach ($cats_v2 as $k=>$v){
            $post['cats_v2'][$k]['cat_id'] = $v;
        }
        $post['extra_service.seven_day_return'] = 1;
        $post['extra_service.freight_insurance'] = 0;
        $post['sku'] = [];
        $skulist = Db::name('shop_goods_sku')->where('goods_id',$param['id'])->select();
        foreach ($skulist as $key=>$sku){
            $post['sku'][$key]['out_sku_id'] = $sku['id'];
            $post['sku'][$key]['thumb_img'] = $domain.$sku['image'];
            $post['sku'][$key]['sale_price'] = $sku['price'];
            $post['sku'][$key]['stock_num'] = $sku['stock'];
            $post['sku'][$key]['sku_code'] = $sku['sku_code'];
            $post['sku'][$key]['sku_attrs'] = [
                ['attr_key'=>'规格','attr_value'=>$sku['spec']],
                ['attr_key'=>'重量','attr_value'=>$sku['weight']],
                ['attr_key'=>'体积','attr_value'=>$sku['volume']],
            ];

        }
        $res = $this->post($url,json_encode($post,320));
        dump($res);die();
    }

    public function classificationTree()
    {
        $url = 'https://api.weixin.qq.com/channels/ec/category/all?access_token='.$this->access_token;
        $res = $this->get($url);

    }

}